<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\IntegralSet;
use App\Services\IntegralSetService;
use Illuminate\Http\Request;


/**
 * 积分设置
 * Class IntegralSetController
 * @package App\Http\Controllers\Estate
 */
class IntegralSetController extends Controller
{


    /**
     * @param Request $request
     * @param IntegralSet $set_model
     * @return array
     */
    public function index(Request $request)
    {
        $list = IntegralSetService::getSetList();
        return $this->success($list);
    }


    /**
     * @param Request $request
     */
    public function store(Request $request, IntegralSet $set_model)
    {

        $data = [
            'type' => $request->type ?? 1,                                        // 类型 1=>商家分类,2=>商家
            'belong_id' => $request->belong_id ?? '',                             // 属于某个商家分类或者是商家ID
            'scenes_type' => implode(',', $request->scenes_type) ?? '',   // 场景类型,1=>用户下单,2=>用户收货
            'state' => $request->state ?? '',                                    // 是否开启 1=>开启,2=>关闭
            'integral' => $request->integral ?? 0
        ];
        $set_model->create($data);
        return $this->success([], __('base.success'));

    }


    /**
     * @param IntegralGoods $ig_model
     * @param $id
     */
    public function show(IntegralSet $set_model, $id)
    {
        $info = $set_model->find($id);
        $info['scenes_type'] = explode(',', $info['scenes_type']);
        return $this->success($info);
    }


    /**
     * @param Request $request
     * @param IntegralSet $set_model
     * @param $id
     */
    public function update(Request $request, IntegralSet $set_model, $id)
    {


        $model = $set_model->find($id);
        // 类型
        if (isset($request->type) && !empty($request->type)) {
            $model->type = $request->type;
        }

        if (isset($request->belong_id) && !empty($request->belong_id)) {
            $model->belong_id = $request->belong_id;
        }

        if (isset($request->scenes_type) && !empty($request->scenes_type)) {
            $model->scenes_type = implode(',', $request->scenes_type ?? []);
        }

        if (isset($request->state) && !empty($request->state)) {
            $model->state = $request->state;
        }

        if (isset($request->integral) && !empty($request->integral)) {
            $model->integral = $request->integral;
        }

        $model->save();
        return $this->success([], __('base.success'));
    }


    /**
     * @param IntegralSet $set_model
     * @param $id
     * @return array
     */
    public function destroy(IntegralSet $set_model, $id)
    {
        $idArray = array_filter(explode(',', $id), function ($item) {
            return is_numeric($item);
        });
        $set_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }

    /**
     * @return array[]
     */
    public function scenesType()
    {

        return [
            ['label' => '用戶下单', 'value' => 1],
            ['label' => '用戶收货', 'value' => 2],
        ];

    }
}

